// packageF/sachertorte/sachertorteBand/sachertorteBand.js
const skinBehavior = require('../../../utils/themeSkin/skinBehavior');
let location = require("../../../mybehaviors/location/location");
const app = getApp();
Page({
  behaviors: [location, skinBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    totalPage: 0,
    isLoadMore: true,
    city: "",
    point: {},
    recommandList: [],
    keyword: '',
    background_color: '#ffc11e'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.setNavigationBarTitle({
      title: '品牌馆',
    })
    if (options.city) {
      this.setData({
        city: options.city
      })
    }
    try {
      this._getLocation((mapdata, e) => {
        console.log(mapdata, e)
        let city = mapdata.address_component.city || '';
        if (this.data.city == '') {
          this.setData({
            city: city
          })
        }
        this.getBand();
      })
    } catch (error) {
      this.getBand();
    }

  },
  bandDetail(event) {
    let item = event.currentTarget.dataset.item;
    wx.navigateTo({
      url: `/packageF/sachertorte/sachertorteBandShop/sachertorteBandShop?brand_id=${item.id}&city=${this.data.city}`,
    })
  },
  goodsNav(event) {
    let id = event.currentTarget.dataset.id || '';
    wx.navigateTo({
      url: `/packageF/sachertorte/sachertorteGoods/sachertorteGoods?id=${id}&city=${this.data.city}`
    })
  },
  init() {
    this.setData({
      page: 1,
      total_page: 0,
      isLoadMore: true,
      recommandList: []
    })
  },
  getBand() {
    this.init();
    let json = {
      city: this.data.city,
      page: this.data.page,
      lng: this.data.point.lng ? this.data.point.lng : "",
      lat: this.data.point.lat ? this.data.point.lat : "",
      brand_name: this.data.keyword
    };
    let urlStr = app.getNetAddresss("plugin.yz-supply-cake.frontend.goods.goods.getBrandAndGoodsAndStore");
    app._getNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          this.data.isLoadMore = true;
          this.data.total_page = res.data.last_page;
          if (!this.data.total_page) {
            this.data.page = 0;
          }
          this.setData({
            recommandList: res.data.data,
            total_page: res.data.last_page,
          })
          let info_basic = wx.getStorageSync('yz_basic_info');
          this.setData({
            background_color: info_basic.theme_color
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData()
    } else {
      console.log('没有更多了')
    }
  },
  getMoreData() {
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.setData({
        page: this.data.page + 1
      })
      let json = {
        city: this.data.city,
        page: this.data.page,
        lng: this.data.point.lng ? this.data.point.lng : '',
        lat: this.data.point.lat ? this.data.point.lat : '',
        brand_name: this.data.keyword
      }
      let urlStr = app.getNetAddresss("plugin.yz-supply-cake.frontend.goods.goods.getBrandAndGoodsAndStore");
      app._getNetWork({
        url: urlStr,
        data: json,
        success: (resdata) => {
          let res = resdata.data
          if (res.result == 1) {
            this.data.isLoadMore = true;
            console.log(res, 'ddddddddd')
            let myData = res.data.data;
            this.setData({
              recommandList: [...this.data.recommandList, ...myData]
            })
          } else {
            this.setData({
              page: this.data.page - 1
            })
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})